/**
	Pulsar engine. Samples.
	Base sample 3.

	Copyright (C) 2013-2014 Dmitry Dolgov

	This program is free software; you can redistribute it and/or modify it under the terms of the
	GNU General Public License as published by the Free Software Foundation; either version 2 of
	the License, or (at your option) any later version.

	This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
	without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
	See the GNU General Public License for more details.
*/

#include <pulsar/pulsar.h>

#include "sample3.h"

namespace sample
{

App App::app;

} // namespace sample

/*
	Preliminary initialization routines
*/
void sample::App::preInit(pl::StartupFunc& info)
{
	//
	// Windows specific functions (members of pl::StartupFunc class)
	//

	// Allow system to display debug classes. This function has effect in PL_DEBUG/PL_RELEASE mode only
	// All debug windows are allowed by default
	// To disable some windows use info.setDebugWindows() call with limited bit set
	info.setDebugWindows(pl::DEBUG_WINDOW_ASSERT | pl::DEBUG_WINDOW_MEMERR | pl::DEBUG_WINDOW_OUTMEM | pl::DEBUG_WINDOW_FATAL_ERR);
	// Initialize common controls and load additional libraries
	info.setCommonControls(pl::COMMON_CONTROLS_ALL_CLASSES | pl::COMMON_CONTROLS_RICHEDIT2);
	// Enable Ctrl+C ewtc signals trap
	info.enableSignals();
	// Trap Win32 exceptions
	info.registerExceptionFilter();

	//
	// Common functions (members of pl::StartupFuncBase class)
	//
	// Set global allocation interface
	info.setGlobalAlloc(NULL); // TODO

	// Set frame memory (per frame stackable allocation with dicard)
	info.setFrameAllocSize(1024*1024);

	// Set stress memory manager options - number of max blocks (1000) and maximum allocation size (20 Mb)
	// This function works in PL_MEMORY_DEBUG mode only
	info.memDebugSetStressAlloc(10000, 20*1024*1024);

	// Set debug memory options:
	// malloc(0) will return stub address (common for all allocations)
	// free(NULL) will cause memory error window
	// memalign() memory can be free with standard free() call
	// Cross-thread allocations/deallocations are prohibited
	// Footer size for each block (buffer overrun check) will be 16 bytes
	info.memDebugSetOptions(pl::MEMOPTIONS_ZEROALLOC_STUB | pl::MEMOPTIONS_FREEMODE_NULLBREAK | pl::MEMOPTIONS_MEMALIGN_CANFREE | pl::MEMOPTIONS_THREADS_DISABLE, 16);

	// Application will be executed on 1st CPU only
	info.setCpuAffinity(1);

	// Setup maximum concurrent threads with logging (default: 20)
	info.setMaxLogThreads(50);

	return;
}

/*
	Application entry point
*/
int sample::App::mainLoop()
{
	// Create standard log file
	// Log file will be located at the same folder with executable
	// All plLogXxx() events will be forwarded to the file while tracertFile variable is exist
	// You may either use standard log tracerts (file, stdout, etc) or create your own log tracerts
	pl::LogTracerFile tracertFile(PL_T("exe:\\sample3.log"));

	plLogInfo(SAMPLE_LIB_NAME, PL_T("Example of log file"));
	plLogInfo(SAMPLE_LIB_NAME, PL_T("Application compilation time: ") << PL_T(__DATE__));

	// Assert examples
	// You may use ASSERT events in the code. All asserts events will be handled by OS specific core extensions
	// You also can customize ASSERT handling with subscribtion to IAppDebug collection or with App::_onAssert() function definition
	// This assert will work in PL_DEBUG configuration only
	PL_ASSERTW(FALSE, PL_T("Assert example #1 (warning)"));
	// This assert will work in PL_DEBUG and PL_RELEASE configurations
	PL_ASSERTE(FALSE, PL_T("Assert example (error)"));

	// Allocation functions
	// You may use either C (malloc(), calloc(), realloc(), free()) or C++ (PL_NEW PL_DELETE) or custom align (memalign, malloc_align(), free(aling())
	// or OS-spec (static_malloc(), static_free()) functions
	// Debug memory manager checks various allocation scenarios, trap errors, calculate statistics and memleaks

	// PL_STATIC_ASSERT() macro allows you to check compile time expressions
	PL_STATIC_ASSERT(sizeof(pl_uint32) == 4);



	
	return 0;
}

//
// End of file 'sample3.cpp'
//
